<?

$g_datetime_is_dst = (date("I") == 1);
$g_datetime_timezone = date("Z");

function server_time_to_gmt_time($t)
{
	global $g_datetime_timezone;
	return $t - $g_datetime_timezone;
}

function gmt_time()
{
	global $g_datetime_timezone;
	return strtotime("now") - $g_datetime_timezone;
}


function vd(&$v, $die=true)
{
	echo "<pre>";
	var_dump(@$v);
	echo "</pre>";
	if ($die) die();
}

function var_by_name($name)
{
	global $$name;
	return $$name;
}

function html_options_from_list($list, $selected="")
{
	foreach($list as $item) 
	{
		echo '<option value="'.$item.'"'.($item == $selected ? " selected" : "").'>'.
			$item.'</option>'."\n";
	}
}

function html_options_from_map($map, $selected="")
{
	foreach($map as $key => $item) 
	{
		echo '<option value="'.$key.'"'.($key == $selected ? " selected" : "").'>'.
			$item.'</option>'."\n";
	}
}

function html_radio_value($value, $selected)
{
	echo "value='".$value."'".($value == $selected ? " checked" : "");
}

function http_redirect($url)
{
	header("Location: $url");
	exit();
}

function generate_html_redirect($url)
{
	echo "<html>\n";
	echo "<head>\n";
	echo "\t<meta http-equiv=\"REFRESH\" content=\"0; url=".$url."\">\n";
	echo "</head>\n";
	echo "\n";
	echo "<body>\n";
	echo "\n";
	echo "</body>\n";
	echo "</html>\n";
	exit();
}

function int_to_hex($n, $digits=1)
{
	$s = "";
	while ($n > 0)
	{
		$d = $n % 16;
		$n = $n >> 4;
		
		if ($d >= 10) $d = chr(ord("A") + $d-10);
		
		$s = $d . $s;
	}
	
	while (strlen($s) < $digits) $s = "0" . $s;
	
	return $s;
}

function generate_uniq_url_param($param_name="uniq_data")
{
	return $param_name."=".md5(uniqid(rand()));
}

function vars_to_url($list)
{
	$s = "";
	foreach ($list as $v)
	{
		global $$v;
		if ($s != "") $s .= "&";
		$s .= $v . "=" . $$v;
	}
	return $s;
}

function set_if_null(&$var, $default)
{
	if ($var == NULL) $var = $default;
}

function get_form_values(&$values, $names)
{
	$values = array();
	foreach ($names as $n) $values[$n] = var_by_name("f_".$n);
	return $values;
}

// old: "TIME_TO_STR_short", "TIME_TO_STR_long"

/* 
Options:
	today_yesterday		- "days" => "today" / "yesterday"
	text_format			- 15:57:12 => 15 hours, 57 minutes, 12 seconds
	max_vals			- max number of values to be displayed
	
	NOT IMPLEMENTED
	short_metrics		- second => sec, minute => min, hour => hr
*/
$g_delta_format_FP = array(
	"today_yesterday"	=> false,
	"text_format"		=> true,
	"max_vals"			=> 1,
);
$g_delta_format_TM = array(
	"today_yesterday"	=> true,
	"text_format"		=> true,
	"max_vals"			=> 3,
);

function delta_time_to_str($x, $format)
{
	if ($x == "") return "";
	$x = round($x);
	
	$xx = $x;
	$s = $xx % 60; $xx = floor($xx / 60);
	$m = $xx % 60; $xx = floor($xx / 60);
	$h = $xx % 24; $xx = floor($xx / 24);
	$d = $xx;

	$a1 = array(); // values
	$a2 = array(); // separators
	
	$skip_d = false;
	if (@$format["today_yesterday"]) 
	{
		// seconds since midnight
		$now = gmt_time();
		$ssm  = $now - mktime(0, 0, 0, date("m",$now), date("d",$now), date("Y",$now));

		if ($x <= $ssm) {
			$skip_d = true;
			$a1[] = "today";
			$a2[] = " ";
		}
		else if ($x <= $ssm + 24*60*60) {
			$skip_d = true;
			$a1[] = "yesterday ";
			$a2[] = " ";
		}
	}
	if (!$skip_d && $d > 0) {
		$a1[] = $d . " day" . ($d > 1 ? "s" : "");
		$a2[] = " ";
	}
	
	if ($h > 0) {
		$a1[] = $h . " hour" . ($h > 1 ? "s" : "");
		$a2[] = ", ";
	}
	if ($m > 0) {
		$a1[] = $m . " minute" . ($m > 1 ? "s" : "");
		$a2[] = ", ";
	}
	if ($s > 0) {
		$a1[] = $s . " second" . ($s > 1 ? "s" : "");
		$a2[] = "";
	}

	// max values
	$mv = (isset($format["max_vals"]) ? $format["max_vals"] : 100);

	$n = count($a1);
	if ($n > $mv) $n = $mv;

	$res = "";
	for ($i = 0; $i < $n; $i++)
	{
		$res .= $a1[$i];
		if ($i+1 < $n) $res .= $a2[$i];
	}

	return $res;
	/*	
	if ($format == TIME_TO_STR_short) {
		return
			($x > 0 ? $x . " days " : "") .
			str_pad($h, 2, "0", STR_PAD_LEFT). ":".
			str_pad($m, 2, "0", STR_PAD_LEFT). ":".
			str_pad($s, 2, "0", STR_PAD_LEFT);
	}
	*/
}

function time_to_str($x, $format)
{
	if ($x == "") return "";
	
	$s = (int)date("s", $x);
	$m = (int)date("i", $x);
	$h = (int)date("H", $x);

	$a1 = array(); // values
	$a2 = array(); // separators
	
	$skip_d = false;
	if (@$format["today_yesterday"]) 
	{
		// seconds since midnight
		$now = gmt_time("now");
		$ssm  = $now - mktime(0, 0, 0, date("m",$now), date("d",$now), date("Y",$now));

		if ($now - $x <= $ssm) {
			$skip_d = true;
			$a1[] = "today";
			$a2[] = " ";
		}
		else if ($now - $x <= $ssm + 24*60*60) {
			$skip_d = true;
			$a1[] = "yesterday ";
			$a2[] = " ";
		}
	}
	
	if (!$skip_d) {
		$a1[] = date("M j, Y", $x);
		$a2[] = " ";
	}
	
	if ($h > 0) {
		$a1[] = $h . " hour" . ($h > 1 ? "s" : "");
		$a2[] = ", ";
	}
	if ($m > 0) {
		$a1[] = $m . " minute" . ($m > 1 ? "s" : "");
		$a2[] = ", ";
	}
	if ($s > 0) {
		$a1[] = $s . " second" . ($s > 1 ? "s" : "");
		$a2[] = "";
	}

	// max values
	$mv = (isset($format["max_vals"]) ? $format["max_vals"] : 100);

	$n = count($a1);
	if ($n > $mv) $n = $mv;

	$res = "";
	for ($i = 0; $i < $n; $i++)
	{
		$res .= $a1[$i];
		if ($i+1 < $n) $res .= $a2[$i];
	}

	return $res;
}



function normalize_url($url)
{
	$debug = false;

	if ($debug) {
		echo "Src  URL: $url\n";
	}

	$components = @parse_url($url);
	if (false) {
		echo "Parsed URL:\n";
		var_dump($components);
	}
	
	$url = "";
	
	// sheme
	if (!isset($components["scheme"])) $components["scheme"] = "http";
	$url = $components["scheme"].":";
	
	// *** workaround *** (in case if sheme is missing)
	// if host is not pressent we should take it from path component
	// example: somedomain.com.ua/jobs.html <-- is a path
	if (!isset($components["host"])) {
		$list = split("/", @$components["path"], 2);
		while (count($list) < 2) array_push($list, "");
		
		$components["host"] = $list[0];
		$components["path"] = "/".$list[1];
	}
	
	// host, port
	$url .= "//".$components["host"];
	if (isset($components["port"])) $url .= ":".$components["port"];
	
	// path
	if (!isset($components["path"])) $components["path"] = "/";
	$url .= $components["path"];
	
	// query
	if (isset($components["query"])) $url .= "?".$components["query"];
	
	// fragment
	if (isset($components["fragment"])) $url .= "#".$components["fragment"];
	
	if ($debug) {
		echo "Norm URL: $url\n\n";
	}
	
	return $url;
}

/*
// Test cases
normalize_url("http://www.sdfgsdgf.com");
normalize_url("http://www.sdfgsdgf.com/");
normalize_url("h://www.sdfgsdgf.com");
normalize_url("h:/www.sdfgsd/gf.com");
normalize_url("http://www.sdfgsdgf.com/index.php?asd=dgf&dfhg=sfg");
normalize_url("sdfgsdgf.com/index.php?asd=dgf&dfhg=sfg#dfgdfg");
normalize_url("www.sdfgsdgf.com:8080/sdf.html");
*/



function domain_name_from_url($url)
{
	$url = normalize_url($url);
	$components = @parse_url($url);
	
	if (!isset($components["host"])) $components["host"] = "";
	
	return $components["host"];
}

function print_time_control($t, $vn)
{
	global $g_am_pm_map;
	
	$list_hours = array();
	for ($i = 0; $i < 12; $i++) $list_hours[] = str_pad($i, 2, "0", STR_PAD_LEFT);
	
	$list_minutes = array();
	for ($i = 0; $i < 60; $i++) if ($i % 15 == 0) $list_mins[] = str_pad($i, 2, "0", STR_PAD_LEFT);

	$am = TIME_AM;
	if ($t > TIME_AMPM_DIVIDER)
	{
		$t -= TIME_AMPM_DIVIDER;
		$am = TIME_PM;
	}
	
	$s = $t % 60; $t = floor($t / 60); 
	$m = $t % 60; $t = floor($t / 60);
	$h = $t % 24; $t = floor($t / 24);
	
	$tcu = "time_control_update('$vn');";
	
	echo '<select id="id_'.$vn.'_h" class="input2" onFocus="this.className=\'input2_act\'" onBlur="this.className=\'input2\'" '.
			'onChange="'.$tcu.'">'."\n";
	html_options_from_list($list_hours, $h);
	echo "</select>\n";
	
	echo '<select id="id_'.$vn.'_am" class="input2" onFocus="this.className=\'input2_act\'" onBlur="this.className=\'input2\'" '.
			'onChange="'.$tcu.'">'."\n";
	html_options_from_map($g_am_pm_map, $am);
	echo "</select>\n";

	echo "hrs\n";
	
	echo '<select id="id_'.$vn.'_m" class="input2" onFocus="this.className=\'input2_act\'" onBlur="this.className=\'input2\'" '.
			'onChange="'.$tcu.'">'."\n";
	html_options_from_list($list_mins, $m);
	echo "</select>\n";

	echo "mins\n";
}

?>